﻿using hf_plat_core;

namespace hf_plat_demo
{
	internal class KDJTB : Indicator
	{
		private readonly DataSeries C;
		private readonly DataSeries HH;
		private readonly DataSeries LL;

		private readonly int Length;
		private readonly int SlowLength;
		private readonly int SmoothLength;
		private readonly DataSeries SumCL;
		private readonly DataSeries SumHL;
		private DataSeries CL;
		public DataSeries DValue;
		private DataSeries HL;
		public DataSeries JValue;
		public DataSeries KValue;

		public KDJTB(int length, int slow, int smooth, DataSeries h, DataSeries l, DataSeries c) : base(c, length, slow, smooth)
		{
			this.C = c;
			this.Length = length;
			this.SlowLength = slow;
			this.SmoothLength = smooth;
			this.HH = new Highest(h, this.Length).Value;
			this.LL = new Lowest(l, this.Length).Value; //计算最高最低
			// TB算法
			this.SumHL = new SUM(this.HL, this.SlowLength).Value; //最高求和
			this.SumCL = new SUM(this.CL, this.SlowLength).Value; //最低求和
			this.DValue = new SMA(this.KValue, this.SmoothLength).Value; //K值序列
		}

		public override void OnBarUpdate()
		{
			this.HL[0] = this.HH[0] - this.LL[0];
			this.CL[0] = this.C[0] - this.LL[0];
			this.KValue[0] = this.SumCL[0]/this.SumHL[0]*100; //K值
			if (double.IsNaN(this.KValue[0]))
			{
				this.KValue[0] = 0;
			}
			this.DValue[0] = this.DValue[0];
			if (double.IsNaN(this.DValue[0]))
			{
				this.DValue[0] = 0;
			}
			this.JValue[0] = 3*this.KValue[0] - 2*this.DValue[0]; //J值
		}
	}
}
